package com.kangdi.riggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kangdi.riggie.common.BaseContext;
import com.kangdi.riggie.common.Result;
import com.kangdi.riggie.entity.AddressBook;
import com.kangdi.riggie.service.AddressBookService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @GetMapping("list")
    public Result<List<AddressBook>> list(){
        Long id = BaseContext.get();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,id);
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return Result.success(list);
    }
    @PostMapping
    public Result<String> save(@RequestBody AddressBook addressBook){
        Long id = BaseContext.get();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,id);
        queryWrapper.eq(AddressBook::getIsDefault,1);
        int count = addressBookService.count(queryWrapper);
        addressBook.setUserId(id);
        if (count==0){
            addressBook.setIsDefault(1);
        }
        addressBookService.save(addressBook);
        return Result.success("添加成功");
    }
    @PutMapping("default")
    public Result<String> updateDefault(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.get());
        queryWrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(queryWrapper);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return Result.success("更新成功");
    }
    @GetMapping("{id}")
    public Result<AddressBook> getById(@PathVariable long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }
    @PutMapping()
    public Result<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return Result.success("更改成功");
    }
    @DeleteMapping
    public Result<String> delete( long ids){
        AddressBook addressBook = addressBookService.getById(ids);
        addressBookService.removeById(ids);
        if (addressBook.getIsDefault()==1){
            LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AddressBook::getUserId,BaseContext.get()).orderByDesc(AddressBook::getUpdateTime).last("limit 1");
            AddressBook newAddressBook = addressBookService.getOne(queryWrapper);
            newAddressBook.setIsDefault(1);
            addressBookService.updateById(newAddressBook);
        }
        return Result.success("删除成功");
    }
    @GetMapping("default")
    public Result<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.get());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        return Result.success(addressBook);
    }
}
